import { useRemoveOrderMutation } from "@store/apiSlice/consultApiSlice";
import { Dialog } from "antd-mobile";
import { useTypedDispatch } from "@store/index";
import { removeOrder } from "@store/slices/fastRecordSlice";

export default function RemoveFastOrder({ id }: Props) {
  // 用于实现删除订单
  const [deleteOrderRequest] = useRemoveOrderMutation();
  // 获取 dispatch 方法
  const dispatch = useTypedDispatch();
  // 删除订单按钮时执行
  const deleteOrderHandler = async () => {
    const confirm = await Dialog.confirm({
      title: "温馨提示",
      content: "您确定要删除该订单吗?",
    });
    // 如果用户点击了取消 阻止程序继续运行
    if (!confirm) return;
    // 用户点击了确认 执行删除订单的请求
    await deleteOrderRequest(id);
    // 删除本地订单
    dispatch(removeOrder(id));
  };
  return <button onClick={deleteOrderHandler}>删除订单</button>;